package com.example.springbootgoods.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootgoods.entity.Commodity;
import com.example.springbootgoods.mapper.CommodityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CommodityService {
    @Autowired
    CommodityMapper commodityMapper;

    /**
     * 分页查询商品信息
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public Page<Commodity> selectPage(Integer pageIndex,Integer pageSize){
        QueryWrapper<Commodity> queryWrapper=new QueryWrapper<>();
        queryWrapper.groupBy("cid");
        queryWrapper.orderByDesc("price");
        Page<Commodity> page = new Page<>(pageIndex,pageSize);
        Page<Commodity> commodityPage= commodityMapper.selectByPage(page, queryWrapper);
        return  commodityPage;
    }

    /**
     * 根据id查询商品详情
     * @param cid
     * @return
     */
    public Commodity selectById(int cid){
        return commodityMapper.selectById(cid);
    }

    /**
     * 新增商品
     * @param commodity
     * @return
     */
    public int insert(Commodity commodity){
        return commodityMapper.insert(commodity);
    }

}
